// @ts-nocheck
// vim: set filetype=typescript:

describe("misc", () => {
  describe("validates compatibility between sortSideEffects and groups configuration", () => {
    function createRule(
      groups: Options[0]["groups"],
      sortSideEffects: boolean = false,
    ): RuleListener {
      return rule.create({
        options: [
          {
            sortSideEffects,
            groups,
          },
        ],
      } as Readonly<RuleContext<MessageId, Options>>);
    }

    let expectedThrownError =
      "Side effect groups cannot be nested with non side effect groups when 'sortSideEffects' is 'false'.";

    it("throws error when side-effect group is nested with non-side-effect groups", () => {
      expect(() =>
        createRule(["external", ["side-effect", "internal"]]),
      ).toThrow(expectedThrownError);
    });

    it("throws error when side-effect-style group is nested with non-side-effect groups", () => {
      expect(() =>
        createRule(["external", ["side-effect-style", "internal"]]),
      ).toThrow(expectedThrownError);
    });

    it("throws error when mixed side-effect groups are nested with non-side-effect groups", () => {
      expect(() =>
        createRule([
          "external",
          ["side-effect-style", "internal", "side-effect"],
        ]),
      ).toThrow(expectedThrownError);
    });

    it("allows side-effect groups to be nested together", () => {
      expect(() =>
        createRule(["external", ["side-effect-style", "side-effect"]]),
      ).not.toThrow(expectedThrownError);
    });

    it("allows any group nesting when sortSideEffects is enabled", () => {
      expect(() =>
        createRule(
          ["external", ["side-effect-style", "internal", "side-effect"]],
          true,
        ),
      ).not.toThrow(expectedThrownError);
    });
  });


  it("respects ESLint disable comments when sorting imports", async () => {
    await valid({
      code: dedent`
          import { b } from "./b"
          import { c } from "./c"
          // eslint-disable-next-line
          import { a } from "./a"
        `,
    });

    await invalid({
      output: dedent`
          import { b } from './b'
          import { c } from './c'
          // eslint-disable-next-line
          import { a } from './a'
        `,
      code: dedent`
          import { c } from './c'
          import { b } from './b'
          // eslint-disable-next-line
          import { a } from './a'
        `,
      options: [{}],
    });

    await invalid({
      output: dedent`
          import { b } from './b'
          import { c } from './c'
          import { a } from './a' // eslint-disable-line
        `,
      code: dedent`
          import { c } from './c'
          import { b } from './b'
          import { a } from './a' // eslint-disable-line
        `,
      options: [{}],
    });

    await invalid({
      output: dedent`
          import { b } from './b'
          import { c } from './c'
          /* eslint-disable-next-line */
          import { a } from './a'
        `,
      code: dedent`
          import { c } from './c'
          import { b } from './b'
          /* eslint-disable-next-line */
          import { a } from './a'
        `,
      options: [{}],
    });

    await invalid({
      output: dedent`
          import { b } from './b'
          import { c } from './c'
          import { a } from './a' /* eslint-disable-line */
        `,
      code: dedent`
          import { c } from './c'
          import { b } from './b'
          import { a } from './a' /* eslint-disable-line */
        `,
      options: [{}],
    });

    await invalid({
      output: dedent`
          import { a } from './a'
          import { d } from './d'
          /* eslint-disable */
          import { c } from './c'
          import { b } from './b'
          // Shouldn't move
          /* eslint-enable */
          import { e } from './e'
        `,
      code: dedent`
          import { d } from './d'
          import { e } from './e'
          /* eslint-disable */
          import { c } from './c'
          import { b } from './b'
          // Shouldn't move
          /* eslint-enable */
          import { a } from './a'
        `,
      options: [{}],
    });

    await invalid({
      output: dedent`
          import { b } from './b'
          import { c } from './c'
          // eslint-disable-next-line rule-to-test/sort-imports
          import { a } from './a'
        `,
      code: dedent`
          import { c } from './c'
          import { b } from './b'
          // eslint-disable-next-line rule-to-test/sort-imports
          import { a } from './a'
        `,
      options: [{}],
    });

    await invalid({
      output: dedent`
          import { b } from './b'
          import { c } from './c'
          import { a } from './a' // eslint-disable-line rule-to-test/sort-imports
        `,
      code: dedent`
          import { c } from './c'
          import { b } from './b'
          import { a } from './a' // eslint-disable-line rule-to-test/sort-imports
        `,
      options: [{}],
    });

    await invalid({
      output: dedent`
          import { b } from './b'
          import { c } from './c'
          /* eslint-disable-next-line rule-to-test/sort-imports */
          import { a } from './a'
        `,
      code: dedent`
          import { c } from './c'
          import { b } from './b'
          /* eslint-disable-next-line rule-to-test/sort-imports */
          import { a } from './a'
        `,
      options: [{}],
    });

    await invalid({
      output: dedent`
          import { b } from './b'
          import { c } from './c'
          import { a } from './a' /* eslint-disable-line rule-to-test/sort-imports */
        `,
      code: dedent`
          import { c } from './c'
          import { b } from './b'
          import { a } from './a' /* eslint-disable-line rule-to-test/sort-imports */
        `,
      options: [{}],
    });

    await invalid({
      output: dedent`
          import { a } from './a'
          import { d } from './d'
          /* eslint-disable rule-to-test/sort-imports */
          import { c } from './c'
          import { b } from './b'
          // Shouldn't move
          /* eslint-enable */
          import { e } from './e'
        `,
      code: dedent`
          import { d } from './d'
          import { e } from './e'
          /* eslint-disable rule-to-test/sort-imports */
          import { c } from './c'
          import { b } from './b'
          // Shouldn't move
          /* eslint-enable */
          import { a } from './a'
        `,
      options: [{}],
    });
  });
});
